<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工姓名" prop="empName">
        <el-input
          v-model="queryParams.empName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="jobNum">
        <el-input
          v-model="queryParams.jobNum"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考勤日期" prop="attendanceDate">
        <el-date-picker clearable
          v-model="queryParams.attendanceDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择考勤日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['hr:originalAttend:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['hr:originalAttend:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['hr:originalAttend:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hr:originalAttend:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="originalAttendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="月份" align="center" prop="nyDate" />
      <el-table-column label="员工姓名" align="center" prop="empName" />
      <el-table-column label="考勤组" align="center" prop="team" />
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="工号" align="center" prop="jobNum" />
      <el-table-column label="职位" align="center" prop="position" />
      <el-table-column label="考勤日期" align="center" prop="attendanceDate" width="180"/>
      <el-table-column label="考勤时间" align="center" prop="attendanceTime" width="180"/>
      <el-table-column label="打卡时间" align="center" prop="dkTime" width="180"/>
      <el-table-column label="打卡结果" align="center" prop="dkResult" width="110"/>
      <el-table-column label="打卡地址" align="center" prop="dkAddress" />
      <el-table-column label="打卡备注" align="center" prop="dkRemark" />
      <el-table-column label="打卡异常原因" align="center" prop="exceptionRemark" />
      <el-table-column label="打卡图1" align="center" prop="dkPic1" />
      <el-table-column label="打卡图2" align="center" prop="dkPic2" />
      <el-table-column label="打卡设备" align="center" prop="dkDevice" />
      <el-table-column label="管理员修改备注" align="center" prop="adminUpdateRemark" />
      <el-table-column label="管理员修改备注图片1" align="center" prop="adminUpdateRemarkPic1" />
      <el-table-column label="管理员修改备注图片2" align="center" prop="adminUpdateRemarkPic2" />
      <el-table-column label="管理员修改备注图片3" align="center" prop="adminUpdateRemarkPic3" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['hr:originalAttend:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['hr:originalAttend:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改原始考勤表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工姓名" prop="empName">
          <el-input v-model="form.empName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="考勤组" prop="team">
          <el-input v-model="form.team" placeholder="请输入考勤组" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="工号" prop="jobNum">
          <el-input v-model="form.jobNum" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="考勤日期" prop="attendanceDate">
          <el-date-picker clearable
            v-model="form.attendanceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择考勤日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考勤时间" prop="attendanceTime">
          <el-date-picker clearable
            v-model="form.attendanceTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择考勤时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打卡时间" prop="dkTime">
          <el-date-picker clearable
            v-model="form.dkTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择打卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打卡地址" prop="dkAddress">
          <el-input v-model="form.dkAddress" placeholder="请输入打卡地址" />
        </el-form-item>
        <el-form-item label="打卡备注" prop="dkRemark">
          <el-input v-model="form.dkRemark" placeholder="请输入打卡备注" />
        </el-form-item>
        <el-form-item label="打卡异常原因" prop="exceptionRemark">
          <el-input v-model="form.exceptionRemark" placeholder="请输入打卡异常原因" />
        </el-form-item>
        <el-form-item label="打卡图1" prop="dkPic1">
          <el-input v-model="form.dkPic1" placeholder="请输入打卡图1" />
        </el-form-item>
        <el-form-item label="打卡图2" prop="dkPic2">
          <el-input v-model="form.dkPic2" placeholder="请输入打卡图2" />
        </el-form-item>
        <el-form-item label="打卡设备" prop="dkDevice">
          <el-input v-model="form.dkDevice" placeholder="请输入打卡设备" />
        </el-form-item>
        <el-form-item label="管理员修改备注" prop="adminUpdateRemark">
          <el-input v-model="form.adminUpdateRemark" placeholder="请输入管理员修改备注" />
        </el-form-item>
        <el-form-item label="管理员修改备注图片1" prop="adminUpdateRemarkPic1">
          <el-input v-model="form.adminUpdateRemarkPic1" placeholder="请输入管理员修改备注图片1" />
        </el-form-item>
        <el-form-item label="管理员修改备注图片2" prop="adminUpdateRemarkPic2">
          <el-input v-model="form.adminUpdateRemarkPic2" placeholder="请输入管理员修改备注图片2" />
        </el-form-item>
        <el-form-item label="管理员修改备注图片3" prop="adminUpdateRemarkPic3">
          <el-input v-model="form.adminUpdateRemarkPic3" placeholder="请输入管理员修改备注图片3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考勤月份" prop="nyDate">
          <el-date-picker clearable
                          v-model="form.nyDate"
                          type="month"
                          value-format="yyyy-MM"
                          placeholder="请选择考勤月份">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport+'&nyDate='+this.form.nyDate"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOriginalAttend, getOriginalAttend, delOriginalAttend, addOriginalAttend, updateOriginalAttend } from "@/api/hr/originalAttend";
import {getToken} from "@/utils/auth";

export default {
  name: "OriginalAttend",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原始考勤表表格数据
      originalAttendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gcCode: null,
        gcName: null,
        empName: null,
        team: null,
        department: null,
        jobNum: null,
        position: null,
        userId: null,
        attendanceDate: null,
        attendanceTime: null,
        dkTime: null,
        dkAddress: null,
        dkRemark: null,
        exceptionRemark: null,
        dkPic1: null,
        dkPic2: null,
        dkDevice: null,
        adminUpdateRemark: null,
        adminUpdateRemarkPic1: null,
        adminUpdateRemarkPic2: null,
        adminUpdateRemarkPic3: null
      },
      // 表单参数
      form: {},
      //考勤导入
      upload: {
        // 是否显示弹出层（物料导入）
        open: false,
        // 弹出层标题（物料导入）
        title: "导入原始考勤",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/hr/originalAttend/importData"
      },
      // 表单校验
      rules: {
        nyDate: [
          { required: true, trigger: "blur", message: "请输入月份" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "原始考勤导入";
      this.upload.open = true;
    },
    /** 查询原始考勤表列表 */
    getList() {
      this.loading = true;
      listOriginalAttend(this.queryParams).then(response => {
        this.originalAttendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gcCode: null,
        gcName: null,
        empName: null,
        team: null,
        department: null,
        jobNum: null,
        position: null,
        userId: null,
        attendanceDate: null,
        attendanceTime: null,
        dkTime: null,
        dkAddress: null,
        dkRemark: null,
        exceptionRemark: null,
        dkPic1: null,
        dkPic2: null,
        dkDevice: null,
        adminUpdateRemark: null,
        adminUpdateRemarkPic1: null,
        adminUpdateRemarkPic2: null,
        adminUpdateRemarkPic3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加原始考勤表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOriginalAttend(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改原始考勤表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOriginalAttend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOriginalAttend(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('hr/originalAttend/importTemplate', {
      }, `原始考勤.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs["form"].validate(valid => {
          if (valid) {
            this.$refs.upload.submit();
          }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除原始考勤表编号为"' + ids + '"的数据项？').then(function() {
        return delOriginalAttend(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hr/originalAttend/export', {
        ...this.queryParams
      }, `originalAttend_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
